package org.lex.io.utils;

import java.io.File;
import java.util.Comparator;

/**
 * Sort java.io.File base on:
 * <ol>
 * <li>Folder before File
 * <li>if both file or folder, using alphabet order
 * </ol>
 */
public class FileComparator implements Comparator<File> {
	public static final FileComparator INSTANCE = new FileComparator();

	@Override
	public int compare(File o1, File o2) {
		boolean dir1 = o1.isDirectory();
		boolean dir2 = o2.isDirectory();
		if (dir1 != dir2) {
			return true == dir1 ? -1 : 1;
		} else {
			return o1.compareTo(o1);
		}
	}
}
